//
//  SearchHomeVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/7/9.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "SearchHomeVC.h"
#import "HourseDataManage.h"
#import "config.h"
#import "HomeNewSiteCell.h"
#import "HourseDetailVc.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "NodataCell.h"
#import "StringTool.h"


@interface SearchHomeVC ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) HourseDataManage *dm;
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)NSMutableDictionary *hightDic;
@property (nonatomic,strong)NodataCell *nodataCell;

@property (nonatomic,strong)NSString *keyword;

@end

@implementation SearchHomeVC
@synthesize dm,dataArray,hightDic,nodataCell;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}

-(void)initView{
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.tableView.hidden = YES;
    self.titleView.doneBtn.hidden = YES;
    [self.titleView setMianTitle:@"搜索"];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    dm = [[HourseDataManage alloc]init];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    self.tableView.hidden = NO;
    if (searchText.length >= 1) {
        self.keyword = searchText;
        [self refreshDataWithKeyword:searchText];
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
{
    [self.view endEditing:YES];
}
#pragma tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count;
    }
    
    [self.tableView setContentOffset:CGPointMake(0, 0)];
    [self.tableView setScrollEnabled:NO];
    return 1;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (dataArray.count==0) {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }

    if (dataArray.count > 0 ) {
        
        HomeNewSiteCell *cell = [HomeNewSiteCell cellWithTableView:tableView];
        
        [cell setData:[dataArray objectAtIndex:indexPath.row]];
        
        return cell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (dataArray&&dataArray.count>0) {
        
            return  [HomeNewSiteCell getHight:[dataArray objectAtIndex:indexPath.row] withTableView:tableView];
    }
    return self.tableView.frame.size.height;;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count>0) {

            HourseDetailVc *newSiteVC = [[HourseDetailVc alloc] init];
            newSiteVC.dataMod = [dataArray objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:newSiteVC animated:YES];
        
    }
}

-(void)refreshDataWithKeyword:(NSString *)keyword{

    [self.tableView.footer resetNoMoreData];
    [nodataCell setState:nodataCellStateLoading];
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        ResulteMod *mod =[dm searchPremiseListWithKeyWord:keyword];
//         ResulteMod *mod =[dm refreshDataWithCache:NO];
        
        dataArray = mod.dataMod;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (mod.resltState == ResultNetError) {//网络出错
                
               [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                    [bself.nodataCell setCodeErrorTip:mod.resultCode];;
                
            }else{//返回正常
                
                if(bself.dataArray.count==0){//没数据
                    
                   [bself.nodataCell setState:nodataCellStateNodata];;
                    
                }else{//有数据
                    
//                    [bself.nodataCell setState:nodataCellStateNorMal];
                }
            }
            
            [bself.tableView reloadData];
            

        });
    });
    
}

- (void)loadMore{
    [super loadMore];
    
    [self.tableView.footer resetNoMoreData];
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm LoadMoresearchPremiseListWithKeyWord:self.keyword];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [bself.tableView.footer endRefreshing];
            
            if (mod.resltState == ResultNetError) {//网络出错
                [MBProgressHUD showError:@"网络错误"];
                
            }else if (mod.resltState == ResultFail){//服务器返回的code出错
                
                [MBProgressHUD showError:mod.resultTip];
            }else{
                
                NSMutableArray *array = [[NSMutableArray alloc]initWithArray:mod.dataMod];
                if (array.count == 0 ||array == nil) {
                    [bself.tableView.footer noticeNoMoreData];
                }
                [array addObjectsFromArray:bself.dataArray];
                bself.dataArray = [[NSArray alloc]initWithArray:array];
            }
            
            [bself.tableView reloadData];
            
            
        });
    });
    
}



@end
